package com.stylefeng.guns.modular.manager.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.GoodsUnit;
import com.stylefeng.guns.modular.manager.service.IGoodsUnitService;

import java.util.Objects;

/**
 * 商品计量单位管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 17:15:02
 */
@Controller
@RequestMapping("/goodsUnit")
public class GoodsUnitController extends BaseController {

    private String PREFIX = "/manager/goodsUnit/";

    @Autowired
    private IGoodsUnitService goodsUnitService;

    /**
     * 跳转到商品计量单位管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "goodsUnit.html";
    }

    /**
     * 跳转到添加商品计量单位管理
     */
    @RequestMapping("/goodsUnit_add")
    public String goodsUnitAdd() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Integer userId = ShiroKit.getUser().getId();
                    System.out.println(userId);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        return PREFIX + "goodsUnit_add.html";
    }

    /**
     * 跳转到修改商品计量单位管理
     */
    @RequestMapping("/goodsUnit_update/{goodsUnitId}")
    public String goodsUnitUpdate(@PathVariable Integer goodsUnitId, Model model) {
        GoodsUnit goodsUnit = goodsUnitService.selectById(goodsUnitId);
        model.addAttribute("item",goodsUnit);
        LogObjectHolder.me().set(goodsUnit);
        return PREFIX + "goodsUnit_edit.html";
    }

    /**
     * 获取商品计量单位管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {

        return goodsUnitService.selectList(null);
    }

    /**
     * 新增商品计量单位管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(GoodsUnit goodsUnit) {
        String createBy = Objects.requireNonNull(ShiroKit.getUser()).getAccount();
        goodsUnit.setCreateBy(createBy);
        goodsUnit.setUpdateBy(createBy);
        goodsUnitService.insert(goodsUnit);
        return SUCCESS_TIP;
    }

    /**
     * 删除商品计量单位管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer goodsUnitId) {
        goodsUnitService.deleteById(Long.valueOf(goodsUnitId));
        return SUCCESS_TIP;
    }

    /**
     * 修改商品计量单位管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GoodsUnit goodsUnit) {
        goodsUnitService.updateById(goodsUnit);
        return SUCCESS_TIP;
    }

    /**
     * 商品计量单位管理详情
     */
    @RequestMapping(value = "/detail/{goodsUnitId}")
    @ResponseBody
    public Object detail(@PathVariable("goodsUnitId") Integer goodsUnitId) {
        return goodsUnitService.selectById(goodsUnitId);
    }
}
